﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpdateDesktopDocHtmlContent
{
    /// <summary>
    /// 提取功能编号，存在一个JSON中，其他地方可以拿到这个JSON，然后获得本地址
    /// </summary>
    internal class ProductFuncMatch
    {
        private static Dictionary<string, string> _match=new Dictionary<string, string>();
        private static string m_ip;
        public static void Start()
        {
            _match=new Dictionary<string, string>();
        }
        public static void SetConfig(string ip)
        {
            m_ip = ip;
        }
        public static void ProcessFiles(string LastSelectedDirectory)
        {
            var files = new DirectoryInfo(LastSelectedDirectory).GetFiles("*.html", SearchOption.AllDirectories);
            for (var i = 0; i < files.Length; i++)
            {
                AddFile(files[i].FullName,i);
            }

            SaveRecordFile(LastSelectedDirectory);
        }
        public static void AddFile(string fileName,int index)
        {
            //StringBuilder stringBuilder = new StringBuilder();
            try
            {
                var allContent = File.ReadAllText(fileName);
                var s1 = "<span class=\"pf\">";
                var t = allContent.IndexOf(s1);

                var htmlDir = fileName.IndexOf("html");
                var header = "http://" + m_ip + "/";
                //stringBuilder.AppendLine("header:" + header);
                while (t > 0)
                {
                    //stringBuilder.AppendLine("t:" + t);
                    var funcEnd = allContent.IndexOf("</span>", t);
                    //stringBuilder.AppendLine("funcEnd:" + funcEnd+","+ (funcEnd - t - s1.Length));
                    var funcId = allContent.Substring(t + s1.Length, funcEnd - t - s1.Length);
                    //stringBuilder.AppendLine("funcId:" + funcId);
                    var refStart = allContent.LastIndexOf("href=", t);
                    //stringBuilder.AppendLine("refStart:" + refStart);
                    refStart = refStart + 6;
                    var refEnd = allContent.IndexOf("\"", refStart + 1);
                    //stringBuilder.AppendLine("refEnd:" + refEnd);
                    var refText = allContent.Substring(refStart, refEnd - refStart);
                    //stringBuilder.AppendLine("refText:" + refText);
                    _match.Add(funcId, header + fileName.Substring(htmlDir + 5).Replace('\\', '/')  + refText);
                    //stringBuilder.AppendLine("funcIdValue:" + _match[funcId]);

                    //MessageBox.Show(stringBuilder.ToString());
                    //stringBuilder.Clear();

                    t = allContent.IndexOf(s1, funcEnd); 
                }

            }
            catch (Exception err)
            {
                //MessageBox.Show(stringBuilder.ToString());
                throw;
            }
        }

        private static void SaveRecordFile(string LastSelectedDirectory)
        {
            var contentDir = Path.Combine(LastSelectedDirectory, "productFunc.json");
            var fs = File.Create(contentDir);
            var sw = new StreamWriter(fs);
            sw.WriteLine("{");
            foreach (var item in _match)
            {
                sw.WriteLine("\"" + item.Key + "\":" + "\"" + item.Value + "\"");
            }
            sw.WriteLine("}");
            sw.Flush();
            sw.Close();
            fs.Close();
        }
    }
}
